package com.example.imageelementposition

import android.graphics.Point
import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    lateinit var screenSize: Point
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        screenSize = ScreenUtil.getScreenSize(this)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> printPosition(
                format(event.x / screenSize.x),
                format(event.y / screenSize.y)
            )
        }
        return super.onTouchEvent(event)
    }

    private fun printPosition(xPos: String, yPos: String) {
        textView.text = "$xPos,$yPos"
        Log.v("MainActivity", "points.add(PointF(${xPos}f,${yPos}f))")
    }

    private fun format(value: Float): String {
        return getNoMoreThanTwoDigits(value)
    }

    private fun toast(text: String) {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
    }
}